package com.demo.http.androidnative;

import android.os.AsyncTask;

import com.demo.http.HttpCallback;
import com.demo.http.androidnative.bean.RequestInfo;

import static com.demo.http.androidnative.bean.RequestInfo.Type.GET;

/**
 * Created by yangming on 16-11-16.
 */
public class HttpTask extends AsyncTask<RequestInfo, Void, Void> {

    private HttpCallback mCallback = null;

    private HttpURLConnectionUtil mHttpURLConnectionUtil = null;

    public HttpTask(HttpCallback mCallback) {
        this.mCallback = mCallback;
    }

    @Override
    protected Void doInBackground(RequestInfo... requestInfos) {
        RequestInfo requestInfo = requestInfos[0];
        mHttpURLConnectionUtil = new HttpURLConnectionUtil();
        mHttpURLConnectionUtil.setCallback(mCallback);
        if (GET == requestInfo.getType()) {
            mHttpURLConnectionUtil.get(requestInfo.getHost(), requestInfo.getParams());
        } else {
            mHttpURLConnectionUtil.post(requestInfo.getHost(), requestInfo.getParams());
        }
        return null;
    }
}
